package generatorController

import (
	generatorModel "CommonsProject/models/generatorModel"
	"CommonsProject/pkg/tools/httpTool"
	generatorService "CommonsProject/service/generatorService"
	"errors"
	"github.com/gin-gonic/gin"
	"log"
	http2 "net/http"
	"strconv"
)

// ProduceController  生成控制器
type ProduceController struct {
	httpTool.BaseController
	Service *generatorService.TableProduceService
}

func (controller *ProduceController) Register(engine *gin.Engine) {
	group := engine.Group("/generator")

	{
		group.GET("", controller.SelectTable)
		group.GET(":id", controller.SelectTableById)
		group.POST("", controller.AddTable)
		group.DELETE(":id", controller.RemoveTable)
		group.PUT("", controller.ModifyTable)
		group.GET("/show/:id", controller.ShowProduce)
		group.GET("/download/:id", controller.Download)
	}
}

// AddTable 添加表
func (controller *ProduceController) AddTable(ctx *gin.Context) {
	var table generatorModel.Table
	controller.ShouldBandingJson(ctx, &table)
	controller.Service.AddSysTable(&table)
	ctx.JSON(http2.StatusOK, controller.SuccessMsg("添加生成表成功"))
}

// RemoveTable 删除表
func (controller *ProduceController) RemoveTable(ctx *gin.Context) {
	id, err := strconv.Atoi(ctx.Param("id"))
	if err != nil {
		panic(errors.New("参数错误！" + err.Error()))
	}
	log.Println("id =", id)
	controller.Service.RemoveSysTable(id)
	ctx.JSON(http2.StatusOK, controller.SuccessMsg("删除生成表成功"))
}

// ModifyTable 修改表
func (controller *ProduceController) ModifyTable(ctx *gin.Context) {
	var table generatorModel.Table
	controller.ShouldBandingJson(ctx, &table)
	controller.Service.ModifySysTable(&table)
	ctx.JSON(http2.StatusOK, controller.SuccessMsg("修改生成表成功"))
}

// SelectTableById 通过ID查询生成表
func (controller *ProduceController) SelectTableById(ctx *gin.Context) {
	id, err := strconv.Atoi(ctx.Param("id"))
	if err != nil {
		panic(errors.New("参数错误！"))
	}
	table := controller.Service.FindSysTableById(id)
	ctx.JSON(http2.StatusOK, controller.SuccessData(table))
}

// SelectTable 查询所有的生成表
func (controller *ProduceController) SelectTable(ctx *gin.Context) {
	size, num := controller.GetPage(ctx)
	var table generatorModel.Table
	_ = ctx.ShouldBind(&table)
	pageInfo := controller.Service.FindSysTable(&table, num, size)
	ctx.JSON(http2.StatusOK, controller.SuccessData(pageInfo))
}

// ShowProduce 预览生成
func (controller *ProduceController) ShowProduce(ctx *gin.Context) {
	id, err := strconv.Atoi(ctx.Param("id"))
	if err != nil {
		panic(errors.New("参数错误！"))
	}

	ctx.JSON(http2.StatusOK, controller.SuccessData(controller.Service.ShowProduce(id)))
}

// Download 下载生成代码
func (controller *ProduceController) Download(ctx *gin.Context) {
	id, err := strconv.Atoi(ctx.Param("id"))
	if err != nil {
		panic(errors.New("参数错误！"))
	}

	buf := controller.Service.Download(id)
	ctx.Header("Content-Type", "application/octet-stream")
	ctx.Header("Content-Disposition", "attachment;filename=dpengchen.zip")
	ctx.Header("Content-Transfer-Encoding", "binary")

	// 将缓冲区内容写入到 HTTP 响应中
	if _, err := ctx.Writer.Write(buf.Bytes()); err != nil {
		ctx.String(http2.StatusOK, "下载文件失败")
	}
}
